﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Uniloader.Managers;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace XamlTorrent
{
    /// <summary>
    /// Displays the list of downloads in all transfer managers.
    /// </summary>
    public partial class DownloadList : ItemsControl, INotifyPropertyChanged
    {
        public DownloadList()
        {
            selectedTasks = new List<TransferTask>();
            selectedTasksWrapper = selectedTasks.AsReadOnly();

            InitializeComponent();
        }

        public IEnumerable<TransferTask> SelectedTasks
        {
            get { return selectedTasksWrapper; }
        }

        List<TransferTask> selectedTasks;
        ReadOnlyCollection<TransferTask> selectedTasksWrapper;

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (TransferTask task in e.RemovedItems)
            {
                selectedTasks.Remove(task);
            }
            
            foreach (TransferTask task in e.AddedItems)
            {
                selectedTasks.Add(task);
            }

            OnPropertyChanged("SelectedTasks");
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
